const express = require('express');
const pool = require('../pool.js');
const router = express.Router();
//用户注册
router.post('/reg',(req,res,next)=>{
	//res.send({code:200,msg:'注册成功'});
	let obj = req.body;
	//res.send(obj);
	if(!obj.uname){
		res.json({code:401,msg:'uname不能为空'});
		//阻止往后执行
		return ;
	}
	if(!obj.upwd){
		res.send({code:402,msg:'upwd不能为空'});
		return ;
	}
	if(!obj.email){
		res.send({code:403,msg:'email不能为空'});
		return ;
	}
	if(!obj.phone){
		res.send({code:404,msg:'phone不能为空'});
		return ;
	}
	//验证手机号码格式
	//正则表达式.test(手机号码)
	//如果手机号码格式不正确
	if(!/^1[3-9]\d{9}$/.test(obj.phone)){
		res.send({code:405,msg:'手机号码格式不正确'});
		return ;
	}
	pool.query('select*from xz_user where uname=?',[obj.uname],(ree,result)=>{
		if(result.length===0){
		pool.query('insert into xz_user set ?',[obj],(err,result)=>{
			if(err){
			//如果数据库执行出现了错误,交给下一个错误处理的中间件
			next(err);
			return ;
			} 
			if(result.affectedRows>0){
			res.send({code:200,msg:'reg suc'});
			}else{
			res.send({code:201,msg:'reg err'});
			}
		});
		}else{
		res.send({code:405,msg:'账号已存在'});
		return;
		}
	});
});
//用户登录
router.post('/login',(req,res,next)=>{
		let obj = req.body;
		console.log(obj);
		if (!obj.uname){
		res.json({code:401,msg:'uname不能为空'})
			return;
		}
		if (!obj.upwd){
			res.json({code:401,msg:'upwd不能为空'});
			return;
		}
	pool.query('select*from xz_user where uname=?&&upwd=?',[obj.uname,obj.upwd],(err,result)=>{
	if (err) {			
		next(err);
		return ;}
	if(result.length===0){
			res.send({code:201  ,msg:'密码账号错误'});
		}else{
			res.send({code:200,msg:'成功'});
		}
	});
		
		//res.send({code:200,msg:"登陆成功"});
});
//修改用户
router.put('/',(req,res,next)=>{
	let obj =req.body;
	let i=400
	for(let k in obj){
		i++
		if (!obj[k]){
			res.send({code:i,msg:`${k}不能为空`});
			return;
		}
	}
	pool.query('update xz_user set ? where uid=?',[obj,obj.uid],(err,result)=>{
	if (err){
		next(err);
		return;
	}
	console.log(result);
	if(result.affectedRows){
			res.send({code:200,msg:'修改成功'})
	}else{
			res.send({code:201,msg:'修改失败'})
	}
	});
});
//查找用户
router.get('/:uid',(req,res,next)=>{
	let obj=req.params
	pool.query('select*from xz_user where uid=?',[obj.uid],(err,result)=>{
		if (err){
		next(err);
		return;
		}
		if(result.length>0){
			res.send({code:200,msg:'查询成功',data:result});
		}else{
			res.send({code:201,msg:'查找失败'});
		}
	});
});
//删除用户
router.delete('/:uid',(req,res,next)=>{
	let obj=req.params
	pool.query('delete from xz_user where uid=?',[obj.uid],(err,result)=>{
		if (err){
		next(err);
		return;
		}
		if(result.affectedRows){
			res.send({code:200,msg:'删除成功',data:result})
		}else{
			res.send({code:201,msg:'删除失败'})
		}
	});
});
//用户列表
router.get('/',(req,res,next)=>{
	let obj = req.query
	console.log(obj);
	if (!obj.pno) {
		obj.pno = 1
	}
	if (!obj.count) {
		obj.count = 4
	}
	//计算开始查询的值
	let start = (obj.pno-1)*obj.count;
	console.log(start)
	//把每页的数据量转为数值型
	let size = parseInt(obj.count);
	console.log(size)
	pool.query('select uid,uname,email,phone from xz_user limit ?,?',[start,size],(err,result)=>{
		if(err){
			next(err)
		}
		console.log(result)
		res.send({code:200,msg:"查询成功",data:result})
	});
});

//导出模块
module.exports = router;

